

<style>
</style>
<template>
  <el-main>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入区域名字" suffix-icon="el-icon-search" v-model="keywords"></el-input>
      <el-button style="margin-left: 5px" type="primary" @click="select">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
    </div>
  <el-table
    :data="tableData"
    border
    style="width: 100%">
    <!-- <el-table-column
      fixed
      prop="id"
      label="id"
      align="center">
    </el-table-column> -->
    <el-table-column
      prop="name"
      label="区域名字"
      align="center">
    </el-table-column>
    <el-table-column
      prop="areaId"
      label="区域编码"
      align="center">
    </el-table-column>
    <el-table-column
      prop="remark"
      label="备注"
      align="center">
    </el-table-column>
    <el-table-column
      label="操作"
      align="center">
      <template slot-scope="scope">
        <el-button @click="handleEdit(scope.row)" type="success" size="small">编辑 <i class="el-icon-edit"></i></el-button>
        <el-popconfirm
        style="margin-left: 5px"
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="确定删除吗？"
          @confirm="handleDelete(scope.row.id)"
        >
          <el-button slot="reference" type="danger" size="small">删除 <i class="el-icon-remove-outline"></i></el-button>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
    <div style="padding: 10px 0">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>

  <el-dialog title="区域信息" :visible.sync="dialogFormVisible" width="30%">
    <el-form label-width="80px" size="small" :model="form">
      <el-form-item label="区域名字" 
        prop="name"
        :label-width="formLabelWidth">
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="区域编码" 
        prop="areaId"
        :label-width="formLabelWidth">
        <el-input v-model="form.areaId" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="备注" 
        prop="remark"
        :label-width="formLabelWidth">
        <el-input v-model="form.remark" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
  <el-dialog title="区域信息" :visible.sync="insertDialogFormVisible" width="30%">
    <el-form label-width="80px" size="small" :model="insertForm">
      <el-form-item label="区域名字" 
        prop="name"
        :label-width="formLabelWidth">
        <el-input v-model="insertForm.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="区域编码" 
        prop="areaId"
        :label-width="formLabelWidth">
        <el-input v-model="insertForm.areaId" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="备注" 
        prop="remark"
        :label-width="formLabelWidth">
        <el-input v-model="insertForm.remark" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="insertDialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="insert">确 定</el-button>
    </div>
  </el-dialog>
</el-main>
</template>

<script>
import request from "../utils/request";
export default{
    name: 'Area',
    data() {
      return {
        tableData: [],
        pageSize: 10,
        currentPage: 1,
        total: 0,
        totalPages: 0,
        keywords: '',
        dialogFormVisible: false,
        insertDialogFormVisible: false,
        form: {
          name: "",
          areaId: "",
          remark: "",
        },
        insertForm: {
          name: "",
          areaId: "",
          remark: "",
        },
        formLabelWidth: '80px'
      }
    },
    created() {
      //请求分页查询数据
      this.load();
    },
    methods: {
      handleEdit(row) {
          this.form = row
          this.dialogFormVisible = true
        
      },
      handleDelete(id) {
        request.get("/area/delete?id="+id).then(res => {
          
            this.$message.success("删除成功")
            this.load()
          
        })
      },
      load() {
        request.post("/area/findPage",
          {
            currentPage: this.currentPage,
            pageSize: this.pageSize
          }
          ).then(res => {
          console.log(res)
          this.tableData = res.list
          this.total = res.total
        })
      },
      save() {
        request.post("/area/update", this.form).then(res => {
            this.$message.success("修改成功")
            this.dialogFormVisible = false
            this.load()
        })
      },
      select() {
        request.post("/area/select",this.keywords
        ).then(res => {
          console.log(res)
          this.tableData = res
        })
      },
      handleAdd() {
        this.insertDialogFormVisible = true
        this.form = {}
      },
      insert() {
        request.post("/area/insert",this.insertForm).then(res => {         
            this.$message.success("添加成功")
            this.insertDialogFormVisible = false
            this.load()          
        })
      },
      reset() {
        this.keywords = ""
        this.load()
      },
      handleSizeChange(pageSize) {
        console.log(pageSize)
        this.pageSize = pageSize
        this.load()
      },
      handleCurrentChange(currentPage) {
        console.log(currentPage)
        this.currentPage = currentPage
        this.load()
      }
    }
}
</script>